<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.pagination.php
 * Type:     function
 * Name:     link
 * Purpose:  zwraca linki do pozostalych stron z danego zakresu
 * Dependencies: function.url.php
 * -------------------------------------------------------------
 */

/**
 *
 * @param <type> $params
 * @param TemplateMaster $smarty
 * @return <type>
 */
function smarty_function_pagination($params, &$smarty) {

	$globalParams = $smarty->_tpl_vars['param'];

	if( isset($globalParams[ROUTER_SORT_LABEL]) )
	{
		$params[ROUTER_SORT_LABEL] = $globalParams[ROUTER_SORT_LABEL];
	}

	if( isset($globalParams[ROUTER_DIR_LABEL]) )
	{
		$params[ROUTER_DIR_LABEL] = $globalParams[ROUTER_DIR_LABEL];
	}

	if( !isset($params['next']) ) {
		$params['next'] = PAGE_NEXT_LABEL;
	}

	if( !isset($params['prev']) ) {
		$params['prev'] = PAGE_PREV_LABEL;
	}

	// Jeżeli nie jest podany label to wstawiam current
	if( !isset($params['label']) ) {
		$params['label'] = 'current';
	}


	if( isset($params['count']) ) {
		$count = $params['count'];
	} else {
		throw new Exception( 'Sortowanie: Wymagana ilość elementów - count!');
	}


	if( isset($params['ajax']) ){
		$ajax = $params['ajax'];
	} else {
		$ajax = null;
	}

	if( isset($params['max']) ){
		$max = $params['max'];
	} else {
		$max = 2;
	}

	if( isset($params['number']) ){
		$number = $params['number'];
	} else {
		$number = 0;
	}

	$pageArray = $smarty->_tpl_vars['_page'];

	if(is_array($pageArray)){
		for ($i = 0; $i < $number+1; $i++) {
			$pageArray[$i] = isset($pageArray[$i])? $pageArray[$i] : 1;
		}
	} else {
		$pageArray = array();
		for ($i = 0; $i < $number+1; $i++) {
			$pageArray[$i] = 1;
		}
	}
	// Wybór strony w tabeli o bierzącym numerku.
	$page = $pageArray[$number];

	$page = max(1, $page);

	if ($count <= 1)
		return '';

	$pageArray[$number] =  max(1, $page - 1);
	$params[ROUTER_PAGE_LABEL] = $pageArray; ;
	$ret = '<div class="prev"><a href="' . pagination_generate_url($params, $smarty) . '" rel="nofollow"' . ($page > 1 ? 'class="navButton floatleft ie6marginTop-25px" ' : ' class="navButton floatleft disabled" onclick="return false;"')  . '>' . (isset($params['prev']) ? $params['prev'] : '« Poprzednia') . ($page > 1 ? '</a>' : '</a>') . '&nbsp;</div><div class="pages">';

	if ($page - $max > 1) {
		$pageArray[$number] =  1;
		$params[ROUTER_PAGE_LABEL] = $pageArray; ;
		$ret .= '<a class="navNumber" href="' . pagination_generate_url($params, $smarty) . '" rel="nofollow">1</a> ';
	}

	if($page - $max > 2) {
		$ret .= ' <span class="navNumber2">...</span> ';
	}

	for ($i = max(1, $page - $max) ; $i < $page; $i++) {
		$pageArray[$number] =  $i;
		$params[ROUTER_PAGE_LABEL] = $pageArray; ;
		$ret .= '<a class="navNumber" href="' . pagination_generate_url($params, $smarty) . '">' . $i . '</a>';
	}

	$pageArray[$number] =  $page;
		 $params[ROUTER_PAGE_LABEL] = $pageArray; ;
	$ret .= '<a class="navNumberSelected" href="' . pagination_generate_url($params, $smarty) . '"  onclick="return false;">' .  $page . '</a>';

	for ($i = min($page + 1, $count) ; $i < $page + $max + 1 && $i <= $count && $page != $count; $i++) {
		$pageArray[$number] =  $i;
		$params[ROUTER_PAGE_LABEL] = $pageArray; ;
		$ret .= '<a class="navNumber" href="' . pagination_generate_url($params, $smarty) . '">' . $i . '</a>';
	}


	if($page + $max < $count - 1) {
		$ret .= ' <span class="navNumber2">...</span> ';
	}

	if ($page + $max < $count) {
		$pageArray[$number] =  $count;
		$params[ROUTER_PAGE_LABEL] = $pageArray; ;
		$ret .= '<a class="navNumber" href="' . pagination_generate_url($params, $smarty) . '">' . $count . '</a>';
	}
	
	$pageArray[$number] =  min($page + 1, $count);
	$params[ROUTER_PAGE_LABEL] = $pageArray; ;

	$ret .= '</div><div class="next"><a   href="' . pagination_generate_url($params, $smarty) . '" rel="nofollow"' . ($page < $count ? 'class=" navButton navCorrection floatright"' : ' class="navButton navCorrection floatright disabled" onclick="return false;"') . ">" . (isset($params['next']) ? $params['next'] : 'Następna »') . '</a></div><div class="cb"></div>';

	return $ret;
}

function pagination_generate_url($params, &$smarty) {

        
	include_once(dirname(__FILE__) . '/function.url.php');

	$pageArray = $params[ROUTER_PAGE_LABEL];

	if( array_sum($pageArray) <= count($pageArray))
		$params[ROUTER_PAGE_LABEL] = null;
	else
		$params[ROUTER_PAGE_LABEL] = implode('-', $pageArray);

	return smarty_function_url($params, $smarty);
}
?>